package com.net.redis_plus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.net.redis_plus.mapper.AdminUserPoMapper;
import com.net.redis_plus.pojo.AdminUserPo;
import com.net.redis_plus.pojo.ConditionEx;
import com.net.redis_plus.pojo.QaDetailSearchVo;
import com.net.redis_plus.service.AdminUserPoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AdminUserPoServiceImpl implements AdminUserPoService {


    @Autowired
    private AdminUserPoMapper adminUserPoMapper;

    @Override
    @Cacheable(cacheNames = "adminuser", key="'adminuser_'+#page+'_'+#rows")
    public List<AdminUserPo> listAllUser(int page, int rows) {
        IPage<AdminUserPo> iPage = new Page<>(page,rows);
        //根据分页对象执行数据库查询,之后获取其其他分页数据.
        QueryWrapper<AdminUserPo> queryWrapper = new QueryWrapper<>();

        IPage<AdminUserPo> adminUserPoIPage = adminUserPoMapper.selectPage(iPage, queryWrapper);
        List<AdminUserPo> records = adminUserPoIPage.getRecords();
        System.out.println(records);
        return records;
    }

    @Override
    @CacheEvict(cacheNames = "adminuser", key="'adminuser_1_1'")
    public AdminUserPo add(int id) {
        LambdaQueryWrapper<AdminUserPo> lambda = new QueryWrapper<AdminUserPo>().lambda();

        lambda.eq(AdminUserPo::getAdminId, id).select(AdminUserPo::getAdminId, AdminUserPo::getCreateTime);
        AdminUserPo adminUserPo = adminUserPoMapper.selectOne(lambda);
        int insert = adminUserPoMapper.deleteById(id);
        return adminUserPo;
    }


    public List<AdminUserPo>  findMySearch(Map<String, Object> reqBody) {
        List<AdminUserPo> adminUserPos = adminUserPoMapper.searchMy(reqBody);
        return adminUserPos;
    }


//    @Transactional
    public List<AdminUserPo>  findMySearch(List<ConditionEx> conditionExes) {
        List<ConditionEx> collect = conditionExes.stream().map(u -> ConditionEx.checkSelf(u)).collect(Collectors.toList());
        List<AdminUserPo> adminUserPos = adminUserPoMapper.searchCondition(collect);
        return adminUserPos;
    }

    public void addUser(AdminUserPo adminUserPo) {

        int insert = adminUserPoMapper.insert(adminUserPo);

    }
}
